package com.yidin.lsc;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.mapapi.BMapManager;
import com.yidin.logic.MainService;
import com.yidin.logic.Task;
import com.yidin.logic.httpCon;
import com.yidin.util.FileAdapter;
import com.yidin.util.GPSPoint;
import com.yidin.widget.SmoothViewPager;
import android.os.Bundle;
import android.content.ComponentName;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.Toast;

public class LscActivity extends BaseActivity {
	public static LscActivity self;
	public static String dataDir = null;
	private ViewPager pager;
	private ViewPagerAdapter pagerControl;// 子页面控件
	public static final String APP_PATH = "lsc/";
	public static final String SERVER_DATA_PATH = APP_PATH + "data/temp/";
	public Views[] views;
	private lscManager mylscManager;
	public static boolean canQuit = false;
	private BMapManager mBMapMan;
	private httpCon myHttpCon;
	private String ver = "&pv=1.0&appv=1.0";
	public static String SERVECE_IP = "http://124.237.121.75:8000/webservice.aspx?";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		 Intent it = new Intent(LscActivity.this,Register.class);
//		 startActivityForResult(it, 1);

		setContentView(R.layout.activity_lsc);
		mylscManager = lscManager.getInstance();
		mylscManager.myAccount.name = "hlf";
		mylscManager.self = this;
		mylscManager.gpsManager = GPSPoint.getInstance(this);
		mylscManager.gpsManager.initialization();
		mylscManager.gpsManager.getGPSPoint(this);
		this.findView();
		this.self = this;
		views = new Views[2];
		views[0] = new NavigationPage(this);
		views[1] = new myDynamic(this);
		this.pagerControl = new ViewPagerAdapter(this, views);
		this.pager.setAdapter(this.pagerControl);
		this.pager.setCurrentItem(0);
		mylscManager.pager = this.pager;
		mylscManager.pagerControl = pagerControl;

		// mBMapMan = new BMapManager(getApplication());
		// mBMapMan.init("D7E3959A574AC544C10485C2C998B5D45ACC9F7F", null);
		FileAdapter.mkDir(FileAdapter.getSdCardRootDir() + SERVER_DATA_PATH);
		onCreateMap();
		pager.setOnPageChangeListener(new MyPagerOnPageChangeListener());
		// super.initMapActivity(mBMapMan);

		// mylscManager.mBMapMan = mBMapMan;
		// mylscManager.map = getLayoutInflater().inflate(R.layout.main_map,
		// null);
		//
		// NavigationPage.getCurrentTask(this,LscActivity.class);
		ComponentName cn = startService(new Intent(this, MainService.class));
		// Task task = new Task(Task.TASK_USER_REGISTER, null);
		// MainService.AddNewTask(task);
		
//		String url = "";
//		JSONArray objInterfaceName = new JSONArray();
//		objInterfaceName.put("UserLoadPersonalPage");
//		objInterfaceName.put("UserLoadGiftList");
//		
//		JSONArray objInterfaceCcotent = new JSONArray();
//		JSONObject obj = new JSONObject();
//		try {
//			obj.put("AccountId", "hlf");
//			obj.put("AccountFrom", "hlf");
//			objInterfaceCcotent.put(obj.toString());
//			objInterfaceCcotent.put(obj.toString());
//			url = SERVECE_IP + "Composite=1&a=" + objInterfaceName.toString() + "&p="
//					+ objInterfaceCcotent.toString();
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		double d = mylscManager.gpsManager.GetDistance(100.0000, 80.0000, 100.0000, 80.0000);
	}

	private void findView() {//SmoothViewPager
		this.pager = (ViewPager) findViewById(R.id.ViewPager);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_lsc, menu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ViewPagerAdapter adapter = (ViewPagerAdapter) pager.getAdapter();
			if (!adapter.back(pager.getCurrentItem())) {
				if (!canQuit) {
					canQuit = true;
					Toast toast = Toast.makeText(this, "再按一次返回键退出.",
							Toast.LENGTH_SHORT);
					toast.show();
				} else
					finish();
				return true;
			} else
				return true;
		}
		finish();
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void onDestroy() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		MainService.exitApp(LscActivity.this);
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mBMapMan != null) {
			mBMapMan.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (mBMapMan != null) {
			mBMapMan.start();
		}
		super.onResume();
	}

	public void setMap() {
		super.initMapActivity(mBMapMan);
	}

	public void onCreateMap() {
		mBMapMan = new BMapManager(getApplication());
		mBMapMan.init("D7E3959A574AC544C10485C2C998B5D45ACC9F7F", null);
		// super.initMapActivity(mBMapMan);
		mylscManager.mBMapMan = mBMapMan;
	}

	public void onDestroyMap() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
	}

	/**
	 * ViewPager的PageChangeListener(页面改变的监听器)
	 * 
	 * @author cola 2012-5-24 下午3:14:27
	 */
	private class MyPagerOnPageChangeListener implements OnPageChangeListener {

		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		/**
		 * 滑动ViewPager的时候,让上方的HorizontalScrollView自动切换
		 */
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			switch (position) {
			case 0:
				((NavigationPage) views[0]).destoryIV();
				((NavigationPage) views[0]).setRightImage(NavigationPage
						.takeScreenShot(LscActivity.this,
								((NavigationPage) views[0]).getImageWidth()));
				break;
			case 1:
				// ((NavigationPage) views[0]).setViewGone();
				break;
			}
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == 1) {
			finish();
			super.onActivityResult(requestCode, resultCode, data);
		}
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {

		super.onPostCreate(savedInstanceState);
	}

	/**
	 * 这个事件可以实现当一个Activity完全加载完毕后，然后就执行。
	 */
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
		((NavigationPage) views[0]).setRightImage(NavigationPage
				.takeScreenShot(LscActivity.this,
						((NavigationPage) views[0]).getImageWidth()));
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		dismissProgress();

//		int task = (Integer) param[0];
//		switch (task) {
//		case Task.TASK_MYDYNAMIC_LIST:
			views[1].refresh(param);
//			break;
//		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		switch (newConfig.orientation) {
		// 更改为LANDSCAPE
		case (Configuration.ORIENTATION_LANDSCAPE): {
			// 如果转换为横向屏时，有要做的事，请写在这里
			Intent it = new Intent(this, BaiduMap.class);
			startActivity(it);
			break;
		}
		// 更改为PORTRAIT
		case (Configuration.ORIENTATION_PORTRAIT):
			// 如果转换为竖向屏时，有要做的事，请写在这里
			break;
		}
	}
}
